package graduation.claf.service;

import graduation.claf.dao.BookMapper;
import graduation.claf.dao.InformationMapper;
import graduation.claf.dao.OwnershipMapper;
import graduation.claf.dao.UserMapper;
import graduation.claf.pojo.Book;
import graduation.claf.pojo.Information;
import graduation.claf.pojo.Ownership;
import graduation.claf.pojo.User;
import graduation.claf.service.ifs.IBookService;
import graduation.claf.tools.Constant;
import graduation.claf.tools.ResponseCode;
import graduation.claf.utils.ServerResponse;
import graduation.claf.vo.UserVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

import static graduation.claf.tools.Tool.createUUID;


@Service()
public class BookService implements IBookService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    BookMapper bookMapper;
    @Autowired
    OwnershipMapper ownershipMapper;
    @Autowired
    InformationMapper informationMapper;

    //查找用户所在的账本
    @Override
    public ServerResponse SearchByUser(String userId) {
        if (StringUtils.isBlank(userId)){
            return ServerResponse.createServerResponseByFail(
                    ResponseCode.PARAMETER_NOT_EMPTY.getCode(),
                    ResponseCode.PARAMETER_NOT_EMPTY.getMsg());
        }
        List<Book> books = bookMapper.selectByUser(userId);
        return ServerResponse.createServerResponseBySuccess(
                books,
                ResponseCode.SEARCH_SUCCESS.getMsg());
    }

    //添加新的账本

    @Override
    public ServerResponse AddBook(Book book,String creatorId) {
        book.setId(createUUID());
        Ownership ownership = new Ownership(book.getId(),creatorId,"creator");
        if ((bookMapper.insert(book) != 0)&&(ownershipMapper.insert(ownership) != 0))
            return ServerResponse.createServerResponseBySuccess();
        else {
            return ServerResponse.createServerResponseByFail(
                    ResponseCode.INSERT_FAIL.getCode(),
                    ResponseCode.INSERT_FAIL.getMsg());
        }
    }

    //邀请新成员

    @Override
    public ServerResponse inviteMember(String sendId,String memberName, String bookId) {
        if (userMapper.findByUsername(memberName) == 0 ){
            return ServerResponse.createServerResponseByFail(
                    ResponseCode.USER_NOT_EXISTS.getCode(),
                    ResponseCode.USER_NOT_EXISTS.getMsg());
        }
        User recipient = userMapper.getUserInformationByUsername(memberName);
        User sender = userMapper.selectByPrimaryKey(sendId);
        if (ownershipMapper.IsMembers(bookId,recipient.getId()) != 0){
            return ServerResponse.createServerResponseByFail(
                    ResponseCode.USER_ALREADY_MEMBERS.getCode(),
                    ResponseCode.USER_ALREADY_MEMBERS.getMsg());
        }
        String context = "您的好友"+sender.getUsername()+"邀请您加入他创建的账本。\n请问您是否要加入该账本";
        String data = bookId+"|"+recipient.getId();
        Information information = new Information(createUUID(),
                sendId,recipient.getId(),0,0,
                context,data,0,null,null);
        if (informationMapper.insert(information) == 0){
            return ServerResponse.createServerResponseByFail(
                    ResponseCode.INVITE_FAIL.getCode(),
                    ResponseCode.INVITE_FAIL.getMsg());
        }
        return ServerResponse.createServerResponseBySuccess();
    }
}
